/**
 * Created on Apr 25, 2007
 */
package x.javax.swing;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.HeadlessException;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import jvs.vfs.tools.FileExplorer;
import x.java.io.File;
import x.javax.swing.filechooser.FileSystemView;

/**
 * @author qiangli
 * 
 */
public class JFileChooser extends javax.swing.JFileChooser implements
		ChangeListener {

	private static final long serialVersionUID = 1L;

	private JTabbedPane tabs;

	private FileExplorer explorer;
	
	private boolean classic = false;

	/**
	 * 
	 */
	public JFileChooser() {
		super(new x.javax.swing.filechooser.FileSystemView());
	}

	/**
	 * @param currentDirectory
	 */
	public JFileChooser(java.io.File currentDirectory) {
		super(currentDirectory, new x.javax.swing.filechooser.FileSystemView());
	}

	/**
	 * @param currentDirectoryPath
	 */
	public JFileChooser(String currentDirectoryPath) {
		super(currentDirectoryPath,
				new x.javax.swing.filechooser.FileSystemView());
	}

	/**
	 * @param fsv
	 */
	public JFileChooser(FileSystemView fsv) {
		super((x.javax.swing.filechooser.FileSystemView) fsv);
	}

	/**
	 * @param currentDirectory
	 * @param fsv
	 */
	public JFileChooser(java.io.File currentDirectory, FileSystemView fsv) {
		super(currentDirectory, (x.javax.swing.filechooser.FileSystemView) fsv);
	}

	/**
	 * @param currentDirectoryPath
	 * @param fsv
	 */
	public JFileChooser(String currentDirectoryPath, FileSystemView fsv) {
		super(currentDirectoryPath,
				(x.javax.swing.filechooser.FileSystemView) fsv);
	}
	
	/**
	 * 
	 */
	public JFileChooser(boolean classic) {
		super(new x.javax.swing.filechooser.FileSystemView());
		this.classic = classic;
	}

	/**
	 * @param currentDirectory
	 */
	public JFileChooser(java.io.File currentDirectory, boolean classic) {
		super(currentDirectory, new x.javax.swing.filechooser.FileSystemView());
		this.classic = classic;
	}

	/**
	 * @param currentDirectoryPath
	 */
	public JFileChooser(String currentDirectoryPath, boolean classic) {
		super(currentDirectoryPath,
				new x.javax.swing.filechooser.FileSystemView());
		this.classic = classic;
	}
	

	/**
	 * @param fsv
	 */
	public JFileChooser(FileSystemView fsv,boolean classic) {
		super((x.javax.swing.filechooser.FileSystemView) fsv);
		this.classic = classic;
	}

	/**
	 * @param currentDirectory
	 * @param fsv
	 */
	public JFileChooser(java.io.File currentDirectory, FileSystemView fsv,boolean classic) {
		super(currentDirectory, (x.javax.swing.filechooser.FileSystemView) fsv);
		this.classic = classic;
	}

	/**
	 * @param currentDirectoryPath
	 * @param fsv
	 */
	public JFileChooser(String currentDirectoryPath, FileSystemView fsv,boolean classic) {
		super(currentDirectoryPath,
				(x.javax.swing.filechooser.FileSystemView) fsv);
		this.classic = classic;
	}
	

	/*
	 * (non-Javadoc)
	 * @see javax.swing.JFileChooser#getSelectedFile()
	 */
	public java.io.File getSelectedFile() {
		return (File) super.getSelectedFile();
	}
	
	/*
	 * (non-Javadoc)
	 * @see javax.swing.JFileChooser#getSelectedFiles()
	 */
	public java.io.File[] getSelectedFiles() {
		java.io.File[] files = super.getSelectedFiles();
		File[] sel = new File[files.length];
		System.arraycopy(files, 0, sel, 0, files.length);
		return sel;
	}

	/**
	 * 
	 */
	protected JDialog createDialog(Component parent) throws HeadlessException {
		Frame frame = parent instanceof Frame ? (Frame) parent
				: (Frame) SwingUtilities
						.getAncestorOfClass(Frame.class, parent);

		String title = getUI().getDialogTitle(this);
		getAccessibleContext().setAccessibleDescription(title);

		JDialog dialog = new JDialog(frame, title, true);
		Container contentPane = dialog.getContentPane();
		contentPane.setLayout(new BorderLayout());
		//
		if (classic) {
			contentPane.add(this, BorderLayout.CENTER);
		} else {
			JPanel card1 = new JPanel();
			explorer = new FileExplorer();
			explorer.setPreferredSize(new Dimension(200, 250));
	
			card1.setLayout(new BorderLayout());
			card1.add(this, BorderLayout.CENTER);
	
			tabs = new JTabbedPane();
			tabs.addTab("Chooser", card1);
			tabs.addTab("Explorer", explorer);
			tabs.setTabPlacement(JTabbedPane.BOTTOM);
			tabs.addChangeListener(this);
			contentPane.add(tabs, BorderLayout.CENTER);
		}
		//
		if (JDialog.isDefaultLookAndFeelDecorated()) {
			boolean supportsWindowDecorations = UIManager.getLookAndFeel()
					.getSupportsWindowDecorations();
			if (supportsWindowDecorations) {
				dialog.getRootPane().setWindowDecorationStyle(
						JRootPane.FILE_CHOOSER_DIALOG);
			}
		}

		dialog.pack();
		dialog.setLocationRelativeTo(parent);

		return dialog;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
	 */
	public void stateChanged(ChangeEvent e) {
		if (classic) {
			return;
		}
		File file = null;
		switch (tabs.getSelectedIndex()) {
		case 0:
			file = explorer.getSelectedFile();

			if (file != null) {
				this.setCurrentDirectory(file);
				this.setSelectedFile(file);
			}
		
			break;
		case 1:
			file = (File) this.getSelectedFile();
			if (file != null)
				explorer.setSelectedFile(file);
			break;
		case -1:
		}
	}

	public boolean isClassic() {
		return this.classic;
	}

	public FileExplorer getExplorer() {
		return this.explorer;
	}

	public JTabbedPane getTabs() {
		return this.tabs;
	}
}
